import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Group {
    private String name;
    private List<Student> students = new ArrayList<>();
    public Group(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void addStudent(Student student) {
        students.add(student);
    }
    @Override
    public String toString() {
        return "小组{" +
                "名称='" + name + '\'' +
                ", 学生数量=" + students.size() +
                '}';
    }

    public static class Main {
        public static void main(String[] args) {
            ClassGroup c1 = new ClassGroup("一年级一班");
            Group g1 = new Group("A组");
            Group g2 = new Group("B组");
            c1.addGroup(g1);
            c1.addGroup(g2);
            Student s1 = new Student("小明", 10, g1);
            Student s2 = new Student("小红", 11, g1);
            Student s3 = new Student("小刚", 12, g2);
            Student s4 = new Student("小丽", 13, g2);
            g1.addStudent(s1);
            g1.addStudent(s2);
            g2.addStudent(s3);
            g2.addStudent(s4);
            c1.addStudent(s1);
            c1.addStudent(s2);
            c1.addStudent(s3);
            c1.addStudent(s4);
            System.out.println(c1);
            Group randomGroup = getRandomGroup(c1.getGroups());
            System.out.println("随机抽取的小组: " + randomGroup);
            Student randomStudentFromGroup = getRandomStudent(randomGroup.getStudents());
            System.out.println("随机抽取的学生: " + randomStudentFromGroup);
            Student randomStudentFromClass = getRandomStudent(c1.getAllStudents());
            System.out.println("随机抽取的全班学生: " + randomStudentFromClass);
        }
        private static <T> T getRandomElement(List<T> list) {
            Random random = new Random();
            int index = random.nextInt(list.size());
            return list.get(index);
        }
        private static Group getRandomGroup(List<Group> groups) {
            return getRandomElement(groups);
        }
        private static Student getRandomStudent(List<Student> students) {
            return getRandomElement(students);
        }
    }
}